﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace BmptoPNG_WPF
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {

        }


        static private void SaveBmpAsPNG(string file, bool isChecked)
        {
            Bitmap img = new Bitmap(file);
            if (file.EndsWith(".bmp") || file.EndsWith(".BMP"))
            {
                img.Save(System.IO.Path.GetFileNameWithoutExtension(file) + ".png", ImageFormat.Png);
            }
            if (isChecked)
            {

                File.Delete(file);
            }
        }

        private void btfile_Click(object sender, RoutedEventArgs e)
        {
            string filename = SelectFileWpf();

            SaveBmpAsPNG(filename, (bool)delBmp.IsChecked);

        }
        public string SelectFileWpf()
        {
            var openFileDialog = new Microsoft.Win32.OpenFileDialog()
            {
                Filter = "Text documents (.txt)|*.txt|All files (*.*)|*.*"
            };
            var result = openFileDialog.ShowDialog();
            if (result == true)
            {
                return openFileDialog.FileName;
            }
            else
            {
                return null;
            }
        }
        private void btfolder_Click(object sender, RoutedEventArgs e)
        {
            FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
            if (folderBrowserDialog.ShowDialog() == (System.Windows.Forms.DialogResult.OK))
            {
                string outDir = folderBrowserDialog.SelectedPath;


                DirectoryInfo TheFolder = new DirectoryInfo(outDir);
                folderwork(TheFolder);

                foreach (FileInfo NextFile in TheFolder.GetFiles())
                {
                    SaveBmpAsPNG(NextFile.FullName, (bool)delBmp.IsChecked);
                }

            }
        }

        private void folderwork(DirectoryInfo theFolder)
        {
            foreach (DirectoryInfo NextFolder in theFolder.GetDirectories())
            {

                folderwork(NextFolder);
            }

        }
    }
}
